package com.hmeter.hadoop.hbase;

import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp;

/**
 * Component name: common
 * Utility class used to validate and retrieve HBase comparator used for scan filters
 * 
 * @author Bakir Jusufbegovic
 * @since 0.1
 * @version 0.1
 */
public class HBaseFilterComparator {
	
	private String[] comparators = {"<","<=","=","!=",">=",">"};
	private String comparator = "";
	
	public String getComparator()
	{
		return comparator;
	}
		
	public HBaseFilterComparator() {};
	
	public CompareOp getCompareOp()
	{
		if (comparator.equals("<"))
		{
			return CompareOp.LESS;
		}
		if (comparator.equals("<="))
		{
			return CompareOp.LESS_OR_EQUAL;
		}
		if (comparator.equals("="))
		{
			return CompareOp.EQUAL;
		}
		if (comparator.equals("!="))
		{
			return CompareOp.NOT_EQUAL;
		}
		if (comparator.equals(">="))
		{
			return CompareOp.GREATER_OR_EQUAL;
		}
		if (comparator.equals(">"))
		{
			return CompareOp.GREATER;
		}

		return CompareOp.NO_OP;
	}
	
	public void validateComparatorInFullColumnQualifier(String fullColumnQualifier) throws Exception
	{
		for (String s : comparators)
		{
			if (fullColumnQualifier.contains(s))
			{
				comparator = s;
			}
		}
		
		if (comparator.equals(""))
		{
			throw new Exception("No valid comparator specified in full column qualifier");
		}
	}
}
